package de.wittekind.bundesjugendspiele.client.management.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.SortedSet;

import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Riege;
import de.wittekind.bundesjugendspiele.server.interfaces.RiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler.Gender;

import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;

public class SchuelerSorter {

	static private class DisciplineList {

		protected Discipline end;
		protected Discipline sprnt;
		protected Discipline jmp;
		protected Discipline thrw;
		protected Discipline stoss;

		public DisciplineList(Schueler s) throws IOException {
			end = DisciplineUtils.givePossibleDisciplines(s,
					Discipline.AUSDAUER);
			sprnt = DisciplineUtils.givePossibleDisciplines(s,
					Discipline.SPRINT);
			jmp = DisciplineUtils.givePossibleDisciplines(s, Discipline.SPRUNG);
			thrw = DisciplineUtils.givePossibleDisciplines(s, Discipline.WURF);
			stoss = DisciplineUtils
					.givePossibleDisciplines(s, Discipline.STOSS);
		}

		@Override
		public boolean equals(Object o) {
			if (!(o instanceof DisciplineList))
				return false;
			DisciplineList d = (DisciplineList) o;
			boolean b = true;
			b &= end == d.end;
			b &= sprnt == d.sprnt;
			b &= jmp == d.jmp;
			b &= thrw == d.thrw;
			b &= stoss == d.stoss;
			return b;
		}

	};

	boolean compDiscList(SortedSet<Discipline> discipline) {

		return false;
	}

	public static void sort(ArrayList<Schueler> s, RiegenLoader l)
			throws IOException {

		Tree t = new Tree(null);
		Tree tmp;

		for (Schueler schueler : s) {
			tmp = t;
			Gender g = schueler.getGender();
			if ((tmp.getChild((g))) == null) {
				tmp.addChild(g);
			}
			tmp = tmp.getChild(g);

			DisciplineList dList = new DisciplineList(schueler);
			if ((tmp.getChild(dList)) == null) {
				tmp.addChild(dList);
			}
			tmp = tmp.getChild(dList);

			Integer yearOfBirth = schueler.getBirthday().get(Calendar.YEAR);
			if (tmp.getChild(yearOfBirth) == null) {
				tmp.addChild(yearOfBirth);
			}
			tmp = tmp.getChild(yearOfBirth);

			String form = schueler.getForm();
			if (tmp.getChild(form) == null) {
				tmp.addChild(form);
			}
			tmp = tmp.getChild(form);

			tmp.addChild(schueler);

		}

		// System.out.println("[" + t.toString() + "]");

		TreeIterator iter = t.iterator();

		iter.next();
		iter.nextLower();
		iter.nextLower();
		Tree active = iter.nextLower();
		ArrayList<Schueler> overflow = new ArrayList<Schueler>();
		Riege r = null;

		int observer = 0;

		do {
			if (overflow.size() > 20) {
				r = l.getRiege(l.newRiege());
				Iterator<Schueler> it = overflow.iterator();
				while (it.hasNext()) {
					r.addSchueler(it.next());
					it.remove();
				}
			} else if (iter.children() < 15  && iter.children() != 0) {
				System.out.println(iter.children());
				active = iter.nextLower();
				observer = 0;
				while (iter.hasNext()) {
					overflow.add((Schueler) active.content);
					iter.remove();
					observer++;
					active = iter.next();
				}
				active = iter.next();
				int i = 0;
			} else if (iter.children() < 25) {
				r = l.getRiege(l.newRiege());
				int i = 0;
				active = iter.nextLower();
				do {
					r.addSchueler((Schueler) active.content);
					iter.remove();
					active = iter.next();

					i++;
				} while (iter.hasNext());
				active = iter.next();
				for (; overflow.iterator().hasNext() && i < 25; i++) {
					r.addSchueler(overflow.iterator().next());
					overflow.iterator().remove();
				}
			} else
				if (iter.children() > 25) {
						int am = iter.children();
						active = iter.nextLower();
					for ( int j = 0; am - j > 20; j += 25)  {
						r = l.getRiege(l.newRiege());
						for (int i = 0; i < 25; i++) {
							r.addSchueler((Schueler) active.content);
							active = iter.next();
							iter.remove();
						}
					}
					active = iter.next();
					do {
						overflow.add((Schueler) active.content);
						iter.remove();
						observer++;
						active = iter.next();
					} while (iter.hasNext());
					active = iter.next();
				}
		} while (active != null);
		System.out.print("overflow: ");
		int i = 0;
		r = l.getRiege(l.newRiege());
		for (Schueler schul : overflow) {
			if (i % 25 == 0) {
				r = l.getRiege(l.newRiege());
			}
			r.addSchueler(schul);
		}
		return;
	}
}
